/**
 * Created by MD-3 on 2017/5/25.
 */


function initMngWindow(thisitem) {
    var thisitem = $(thisitem)
    var togstate = thisitem.attr("togstate")
    if("1" == togstate){
        thisitem.attr("togstate","0");
        return;
    }else {
        thisitem.attr("togstate","1");
    }

    getUnitNameHtml();

    getAllDeviceHtml();

  /*  var selDeviceUl = $('#myseldevice-ul');
   var htmlStr2 = getSelDeviceLi();
   selDeviceUl.html(htmlStr2);*/

    var selDeviceUl = $('#myalarmhis-ul');
    var htmlStr3 = getAlarmHisLi();
    selDeviceUl.html(htmlStr3);
}

function getUnitNameHtml(){
    var unitName = mngWindow_mock.unitName;
    var mngUnit = $('#mymng-unit');
    mngUnit.html(unitName);
}

function getAllDeviceHtml() {
    var allDeviceUl = $('#myalldevice-ul');
    var htmlStr1 = getAllDeviceLi();
    allDeviceUl.html(htmlStr1);
    getAllDeviceLiAppend()
}


function getAllDeviceLi() {
    var id, type, name, workState, alarmState, updateState, preDeviceId;
    var i = 0, mDeviceObj;
    var htmlStr = '';
    for (i in deviceMarkerList) {
        mDeviceObj = deviceMarkerList[i];
        id          = mDeviceObj.id;
        type        = mDeviceObj.type;
        name        = mDeviceObj.name;
        workState   = mDeviceObj.workState;
        alarmState  = mDeviceObj.alarmState;
        updateState = mDeviceObj.updateState;
        preDeviceId = mDeviceObj.preDeviceId;

        if("0" == type){
            htmlStr += getMngDevUl(id, name, workState, alarmState, updateState);
        }
    }
    return htmlStr;

  /*
   使用折叠样式
   <div data-toggle="collapse" data-target="#demo">简单的可折叠组件</div>

   <div id="demo" class="collapse in">
   <div class="list-group">
   <a href="#" class="list-group-item">
   <h4 class="list-group-item-heading">
   商务网站包
   </h4>
   </a>
   <a href="#" class="list-group-item">
   <h4 class="list-group-item-heading">
   免费域名注册
   </h4>
   <p class="list-group-item-text">
   您将通过网页进行免费域名注册。
   </p>
   </a>
   <a href="#" class="list-group-item">
   <h4 class="list-group-item-heading">24*7 支持</h4>
   <p class="list-group-item-text">我们提供 24*7 支持。</p>
   </a>
   </div>
   </div>
   * */
}

function getAllDeviceLiAppend() {
    var id, type, name, workState, alarmState, updateState, preDeviceId;
    var i = 0, mDeviceObj;
    var htmlStr = '';
    for (i in deviceMarkerList) {
        mDeviceObj = deviceMarkerList[i];
        id          = mDeviceObj.id;
        type        = mDeviceObj.type;
        name        = mDeviceObj.name;
        workState   = mDeviceObj.workState;
        alarmState  = mDeviceObj.alarmState;
        updateState = mDeviceObj.updateState;
        preDeviceId = mDeviceObj.preDeviceId;

        if("1" == type){
            var id_listGroup = "listgroup_"+preDeviceId;
            htmlStr = getMngDevLi(id, name, workState, alarmState, updateState, preDeviceId);
            $("#"+id_listGroup).append(htmlStr);
        }
    }
}

function getMngDevUl(id, name, workState, alarmState, updateState) {
    var id_MngDevUl = "mngdevul_"+id;
    var id_listGroup = "listgroup_"+id;
    var htmlStr =
        '<div class="alldev-mngdev" data-toggle="collapse" data-target="#'+id_MngDevUl+'">'
        +'    <span>' + id + '</span>'
        +'    <a onclick="mapPanTo(this)" deviceid="' + id + '">' + name + '</a>'
        +'    <span class="caret mymng-caret"></span>'
        + '</div>'
        + '<div id="'+id_MngDevUl+'" class="collapse in alldev-mngdevul">'
        + '    <div class="list-group" id="'+id_listGroup+'">'
        + '   </div>'
        + '</div>';
    return htmlStr;
}

function getMngDevLi(id, name, workState, alarmState, updateState, preDeviceId) {
    var htmlStr = '<div class="alldev-mngdevli">'
        +'    <span>' + id + '</span>'
        +'    <a onclick="mapPanTo(this)" deviceid=' + id + '>' + name + '</a>'
        +'    <span>' + workState + '</span>'
        +'    <span>' + alarmState + '</span>'
        +'    <span>' + updateState + '</span>'
        +'</div>';
    return htmlStr;
}

function getAlarmHisLi() {
    var alarmList = alarmList_mock;
    var i = 0, alarm, htmlStr = "";
    var id, name, lng, lat, alarmId, alarmTime, alarmState, alarmType, mediaUrl;
    for (i in alarmList) {
        alarm = alarmList[i];
        id = alarm.id;
        name = alarm.name;
        lng = alarm.position.lng;
        lat = alarm.position.lat;
        alarmId = alarm.alarmId;
        alarmTime = alarm.alarmTime;
        alarmState = alarm.alarmState;
        alarmType = alarm.alarmType;
        audioUrl = alarm.audioUrl;

        var mediaUrlStr = '<span>---</span>';
        if(audioUrl && ""!=audioUrl){
            mediaUrlStr = '<span><img onclick="playAlarmMedia(this)" src="'+resource_mock.IMG_ALARM_OFF+'" audioUrl=' + audioUrl + ' count="0"/></span>'
        }
        var alarmCheckStr = '---';
        if ("1" == alarmState) {
            alarmCheckStr = '<button type="button" class="btn btn-primary btn-xs" itemid="'+id+'" onclick="openDevice(this)">未处理</button>';
        }else{
            alarmCheckStr = '<span>' + alarmType + '</span>'
        }
        htmlStr += '<li>'
            + '<span>' + alarmId + '</span>'
            + '<a onclick="mapPanTo(this)" deviceid=' + id + '>' + name + '</a>'
            + '<span>' + alarmTime + '</span>'
            + mediaUrlStr
            + alarmCheckStr
            + '</li>';
        console.log("getHisAlarmLi:" + "--------" + audioUrl);
    }
    return htmlStr;
}

function openDevice(thisitem){
    var thisitem = $(thisitem);
    var deviceid = thisitem.attr("itemid");
    var alarmCheck = "1";
    openDeviceInfo(deviceid,alarmCheck)
}


function selectDevice() {
    var detectorEle = document.getElementById('detector');
    var repeaterEle = document.getElementById('repeater');
    var selWorkstateEle = $('#mysel-workstate');
    var selAlarmstateEle = $('#mysel-alarmstate');
    var selUpdatestateEle = $('#mysel-updatestate');

    var detectorChecked = detectorEle.checked == true ? "1" : "9";    //0-中继，1-终端
    var repeaterChecked = repeaterEle.checked == true ? "0" : "9";
    var selWorkstate = selWorkstateEle.val();
    var selAlarmstate = selAlarmstateEle.val();
    var selUpdatestate = selUpdatestateEle.val();
    console.log("终端:" + detectorChecked + "--中继:" + repeaterChecked + "--工作：" + selWorkstate + "--报警：" + selAlarmstate + "--更新：" + selUpdatestate)

    showSelectedDevice(detectorChecked, repeaterChecked, selWorkstate, selAlarmstate, selUpdatestate);
}

function showSelectedDevice(detectorChecked, repeaterChecked, selWorkstate, selAlarmstate, selUpdatestate) {
    if (!detectorChecked || !repeaterChecked || !selWorkstate || !selAlarmstate || !selUpdatestate) {
        return;
    }
    for (var i = 0; i < deviceMarkerList.length; i++) {
        if ((deviceMarkerList[i].type == detectorChecked || deviceMarkerList[i].type == repeaterChecked) &&
            (selWorkstate == "9" || deviceMarkerList[i].workState == selWorkstate) &&
            (selAlarmstate == "9" || deviceMarkerList[i].workState == selAlarmstate) &&
            (selUpdatestate == "9" || deviceMarkerList[i].workState == selUpdatestate)) {
            deviceMarkerList[i].marker.show();
        } else {
            deviceMarkerList[i].marker.hide();
        }
    }
}

function mapPanTo(thisitem) {
    var link = $(thisitem);
    var deviceid = link.attr("deviceid");
    console.log("mapPanTo" + "--------" + deviceid);
    var pisotion = getDeviceById(deviceid).marker.getPosition();
    map.panTo(pisotion);
}
